<script setup lang="ts">
// 使用import type导入类型
import { ref } from 'vue'
import type { Ref } from 'vue'

// 定义Todo类型
type Todo = {
  id: number
  text: string
  completed: boolean
}

// 为ref添加类型注解
const newTodo: Ref<string> = ref('')
const todos: Ref<Todo[]> = ref(JSON.parse(localStorage.getItem('todos') || '[]'))

const addTodo = () => {
  if (newTodo.value.trim()) {
    todos.value.push({
      id: Date.now(),
      text: newTodo.value.trim(),
      completed: false
    })
    newTodo.value = ''
    saveToLocal()
  }
}

const toggleComplete = (id: number) => {
  const todo = todos.value.find(t => t.id === id)
  if (todo) {
    todo.completed = !todo.completed
    saveToLocal()
  }
}

const deleteTodo = (id: number) => {
  todos.value = todos.value.filter(t => t.id !== id)
  saveToLocal()
}

const saveToLocal = () => {
  localStorage.setItem('todos', JSON.stringify(todos.value))
}
</script>

<template>
  <!-- 模板部分保持不变 -->
  <div class="container">
    <h1>Todo List</h1>
    <div class="input-group">
      <input 
        v-model.trim="newTodo"
        @keyup.enter="addTodo"
        placeholder="Add new todo..."
      />
      <button @click="addTodo">Add</button>
    </div>
    <ul>
      <li 
        v-for="todo in todos" 
        :key="todo.id"
        :class="{ completed: todo.completed }"
      >
        <span @click="toggleComplete(todo.id)">{{ todo.text }}</span>
        <button @click="deleteTodo(todo.id)">×</button>
      </li>
    </ul>
  </div>
</template>

<style>
/* 样式部分保持不变 */
body {
  font-family: Arial, sans-serif;
  background-color: #f5f5f5;
  margin: 0;
  padding: 20px;
}

.container {
  max-width: 600px;
  margin: 0 auto;
  background: white;
  padding: 20px;
  border-radius: 8px;
  box-shadow: 0 2px 4px rgba(0,0,0,0.1);
}

.input-group {
  display: flex;
  gap: 10px;
  margin-bottom: 20px;
}

input {
  flex: 1;
  padding: 8px;
  border: 1px solid #ddd;
  border-radius: 4px;
}

button {
  padding: 8px 16px;
  background: #4b566b;
  border: none;
  border-radius: 4px;
  color: white;
  cursor: pointer;
}

button:hover {
  background: #3b4354;
}

ul {
  list-style: none;
  padding: 0;
  margin: 0;
}

li {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 10px;
  border-bottom: 1px solid #eee;
}

li:last-child {
  border-bottom: none;
}

li.completed span {
  text-decoration: line-through;
  color: #888;
}

li button {
  background: #ff4757;
}

li button:hover {
  background: #ff6b81;
}
</style>